import web.ling.por.Application
import web.ling.por.Client
import web.ling.por.FieldOfExpertise
import web.ling.por.Language
import web.ling.por.OrderStatus
import web.ling.por.ProofreadingOrder
import web.ling.por.Provider

class BootStrap {

    def init = { servletContext ->
        if(!OrderStatus.count()) {
            new OrderStatus(description: 'open').save()
            new OrderStatus(description: 'done').save()
            new OrderStatus(description: 'canceled').save()
            new OrderStatus(description: 'inWork').save()
        }

        if(!FieldOfExpertise.count()) {
            new FieldOfExpertise(name: "General", description: "General themes").save()
            new FieldOfExpertise(name: "Tehnical", description: "Experience with proofreading of tehnical texts").save()
            new FieldOfExpertise(name: "Legal", description: "Experience with proofreading of legal texts").save(flush: true)
        }

        if(!Language.count()) {
            new Language(description: 'Slovenian').save(flush:true)
        }

        if(!Client.count()) {
            new Client(name: "Matija Moron", email: "matija.moron@test.si", password: "test").save()
        }

        if(!Provider.count()) {
            new Provider(
                    name: "Dwight Schrute",
                    email: "dwight@test.si",
                    password: "test",
                    nativeLanguage: Language.getAll().first(),
                    languages: [] << Language.getAll().first(),
                    fieldsOfExpertise: FieldOfExpertise.getAll(1,2)
            ).save(flush: true)
        }

        if(!ProofreadingOrder.count()) {
            def po = new ProofreadingOrder(
                    client: Client.getAll().first(),
                    provider: null,
                    orderStatus: OrderStatus.findByDescription('open'),
                    fieldOfExpertise: FieldOfExpertise.findByName('General'),
                    summary: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent consequat purus lorem, nec vestibulum felis dignissim malesuada. Nulla facilisi. Maecenas a aliquam augue. Curabitur ullamcorper erat eget libero luctus blandit. Etiam pretium urna enim, eu malesuada tellus egestas vel nullam sodales.',
                    deadlineDate: new Date().plus(10),
                    submissionDate: new Date(),
                    document: new byte[10],
                    documentType: 'pdf',
                    orderPrice: 10,
                    language: Language.getAll().first()
            )
            po.save(flush:true)

            def app = new Application(order: po, provider: Provider.getAll().first(), canStart: new Date(), canFinish: new Date()+1, price: 55.0)
            po.applyingProviders << app
            po.save()

            new ProofreadingOrder(
                    client: Client.getAll().first(),
                    provider: null,
                    orderStatus: OrderStatus.findByDescription('open'),
                    fieldOfExpertise: FieldOfExpertise.findByName('Tehnical'),
                    summary: 'This is a tehnical order.',
                    deadlineDate: new Date().plus(10),
                    submissionDate: new Date(),
                    document: new byte[10],
                    documentType: 'pdf',
                    orderPrice: 10,
                    language: Language.getAll().first()
            ).save()

        }
    }
    def destroy = {
    }
}
